﻿using System;
using System.Data;
using System.Text;
using FX.CC.Andon.Utility.Common;
using FX.CC.Andon.Utility.DataBase;

namespace FX.CC.Andon.Service.CommManage
{
    public class CommonService : DbHelperBase
    {
        public int CheckedCode(string tableName, string fieldName, string code)
        {
            var strSql = new StringBuilder();
            strSql.Append(string.Format(" select  count(*) AS [count] FROM {0} Where {1} = '{2}' ", tableName, fieldName, code));
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        public bool Freeze(string table, string field, string value)
        {
            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(string.Format(" UPDATE {0} SET RecStatus=0 WHERE {1} = '{2}'", table, field, value));
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public bool Unfrozen(string table, string field, string value)
        {
            bool result = false;
            var strSql = new StringBuilder();
            strSql.Append(string.Format(" UPDATE {0} SET RecStatus=1 WHERE {1} = '{2}'", table, field, value));
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }

        public int CheckedCodes(string tableName, string fieldName1, string fieldName2, string code1, string code2)
        {
            var strSql = new StringBuilder();
            strSql.Append(string.Format(" select  count(*) AS [count] FROM {0} Where {1} = '{2}' and {3}='{4}' ", tableName, fieldName1, code1, fieldName2, code2));
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

        public int CheckedPwd(string loginCode, string pwd)
        {
            var count = 0;
            var strSql = new StringBuilder();
            strSql.Append(" select  count(*) AS [count] FROM UT_User AS A WHERE LoginCode=@LoginCode and LoginPwd=@LoginPwd");
        
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@loginCode", loginCode);
                    Db.AddInParameter(cmd, "@LoginPwd", EncryptPassWord.ConvertStringToBase64String(pwd, Encoding.UTF8));
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["count"]);
                        }
                    }

                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }

      
    }
}
